﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Model;
using System.Collections.Generic;
namespace DAL
{
    /// <summary>
    /// 数据访问类:PMS_ProjectFinance
    /// </summary>
    public class PMS_ProjectFinanceDAL
    {
        public PMS_ProjectFinanceDAL()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.PMS_ProjectFinance model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@projectID", SqlDbType.NVarChar,50),
					new SqlParameter("@customerName", SqlDbType.NVarChar,100),
					new SqlParameter("@signingDate", SqlDbType.DateTime),
					new SqlParameter("@signingAmount", SqlDbType.Float,8),
					new SqlParameter("@changeVOAmount", SqlDbType.Float,8),
					new SqlParameter("@settleAmount", SqlDbType.Float,8),
					new SqlParameter("@payAmount", SqlDbType.Float,8),
					new SqlParameter("@applyAmount", SqlDbType.Float,8),
					new SqlParameter("@noApplyAmount", SqlDbType.Float,8),
					new SqlParameter("@applyStage", SqlDbType.NVarChar,50),
					new SqlParameter("@remark", SqlDbType.NVarChar)};
            parameters[0].Value = model.projectID;
            parameters[1].Value = model.customerName;
            if (model.signingDate == null)
            {
                parameters[2].Value = DBNull.Value;
            }
            else
            {
                parameters[2].Value = model.signingDate;
            }
            if (model.signingAmount == null)
            {
                parameters[3].Value = DBNull.Value;
            }
            else
            {
                parameters[3].Value = model.signingAmount;
            }
            if (model.changeVOAmount == null)
            {
                parameters[4].Value = DBNull.Value;
            }
            else
            {
                parameters[4].Value = model.changeVOAmount;
            }
            if (model.settleAmount == null)
            {
                parameters[5].Value = DBNull.Value;
            }
            else
            {
                parameters[5].Value = model.settleAmount;
            }
            if (model.payAmount == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
            {
                parameters[6].Value = model.payAmount;
            }
            if (model.applyAmount == null)
            {
                parameters[7].Value = DBNull.Value;
            }
            else
            {
                parameters[7].Value = model.applyAmount;
            }
            if (model.noApplyAmount == null)
            {
                parameters[8].Value = DBNull.Value;
            }
            else
            {
                parameters[8].Value = model.noApplyAmount;
            }
            if (string.IsNullOrEmpty(model.applyStage))
            {
                parameters[9].Value = DBNull.Value;
            }
            else
            {
                parameters[9].Value = model.applyStage;
            }
            if (string.IsNullOrEmpty(model.remark))
            {
                parameters[10].Value = DBNull.Value;
            }
            else
            {
                parameters[10].Value = model.remark;
            }

            int row = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.StoredProcedure, "sp_AddProjectFinance", parameters);
            if (row > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.PMS_ProjectFinance model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PMS_ProjectFinance set ");
            strSql.Append("signingDate=@signingDate,");
            strSql.Append("signingAmount=@signingAmount,");
            strSql.Append("changeVOAmount=@changeVOAmount,");
            strSql.Append("settleAmount=@settleAmount,");
            strSql.Append("payAmount=@payAmount,");
            strSql.Append("applyAmount=@applyAmount,");
            strSql.Append("noApplyAmount=@noApplyAmount,");
            strSql.Append("applyStage=@applyStage,");
            strSql.Append("remark=@remark");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int),
					new SqlParameter("@remark", SqlDbType.NVarChar),
					new SqlParameter("@signingDate", SqlDbType.DateTime),
					new SqlParameter("@signingAmount", SqlDbType.Float,8),
					new SqlParameter("@changeVOAmount", SqlDbType.Float,8),
					new SqlParameter("@settleAmount", SqlDbType.Float,8),
					new SqlParameter("@payAmount", SqlDbType.Float,8),
					new SqlParameter("@applyAmount", SqlDbType.Float,8),
					new SqlParameter("@noApplyAmount", SqlDbType.Float,8),
					new SqlParameter("@applyStage", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.id;
            if (model.remark == null)
            {
                parameters[1].Value = DBNull.Value;
            }
            else
            {
                parameters[1].Value = model.remark;
            }
            if (model.signingDate == null)
            {
                parameters[2].Value = DBNull.Value;
            }
            else
            {
                parameters[2].Value = model.signingDate;
            }
            if (model.signingAmount == null)
            {
                parameters[3].Value = DBNull.Value;
            }
            else
            {
                parameters[3].Value = model.signingAmount;
            }
            if (model.changeVOAmount == null)
            {
                parameters[4].Value = DBNull.Value;
            }
            else
            {
                parameters[4].Value = model.changeVOAmount;
            }
            if (model.settleAmount == null)
            {
                parameters[5].Value = DBNull.Value;
            }
            else
            {
                parameters[5].Value = model.settleAmount;
            }
            if (model.payAmount == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
            {
                parameters[6].Value = model.payAmount;
            }
            if (model.applyAmount == null)
            {
                parameters[7].Value = DBNull.Value;
            }
            else
            {
                parameters[7].Value = model.applyAmount;
            }
            if (model.noApplyAmount == null)
            {
                parameters[8].Value = DBNull.Value;
            }
            else
            {
                parameters[8].Value = model.noApplyAmount;
            }
            if (model.applyStage == null)
            {
                parameters[9].Value = DBNull.Value;
            }
            else
            {
                parameters[9].Value = model.applyStage;
            }

            int rows = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PMS_ProjectFinance set isDelete='1' ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = { new SqlParameter("@id", id) };
            int rows = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PMS_ProjectFinance GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select a.*,b.city FROM PMS_ProjectFinance as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.id=@id");
            SqlParameter[] values = { new SqlParameter("@id", id) };
            return SqlHelper.GetDataByColumns<PMS_ProjectFinance>(Comm.Config.PMS, strSql.ToString(), values);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PMS_ProjectFinance> GetList(string projectID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select a.*,b.city FROM PMS_ProjectFinance as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.projectID=@projectID");
            SqlParameter[] values = { new SqlParameter("@projectID", projectID) };
            return SqlHelper.GetDataByColumnsLst<PMS_ProjectFinance>(Comm.Config.PMS, strSql.ToString(), values);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PMS_ProjectFinance> GetList(PMS_ProjectFinanceQuery query)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select a.*,b.city FROM PMS_ProjectFinance as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.projectID=@projectID");
            if (query.startDate != null && query.startDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate>='" + query.startDate + "' ");
            }
            if (query.endDate != null && query.endDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate<='" + query.endDate + "' ");
            }
            SqlParameter[] values = { new SqlParameter("@projectID", query.projectID) };
            return SqlHelper.GetDataByColumnsLst<PMS_ProjectFinance>(Comm.Config.PMS, strSql.ToString(), values);
        }

        /// <summary>
        /// 根据项目编号查询数据
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public DataTable GetListTable(PMS_ProjectFinanceQuery query)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select projectIDS=b.projectID,b.projectName,b.city,a.* FROM PMS_ProjectFinance as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.projectID=@projectID ");
            if (query.startDate != null && query.startDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate>='" + query.startDate + "' ");
            }
            if (query.endDate != null && query.endDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate<='" + query.endDate + "' ");
            }
            SqlParameter[] values = { new SqlParameter("@projectID", query.projectID) };
            return SqlHelper.ExecuteGetTable(Comm.Config.PMS, CommandType.Text, strSql.ToString(), values);
        }

        #endregion  Method
    }
}

